package com.example.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.vote.entity.Record;
import com.example.vote.mapper.RecordMapper;
import com.example.vote.service.BizException;
import com.example.vote.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 投票系统-投票记录表-6 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-14
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Transactional
    @Override
    public void commit(List<Record> records) throws BizException {
        if(records.isEmpty()){
            throw new BizException("请选择选项！");
        }
        //判断是否重复投票
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Record::getVid, records.get(0).getVid());
        lqw.eq(Record::getUsid, records.get(0).getUsid());
        if (this.count(lqw) > 0) {
            throw new BizException("请勿重复投票！");
        }
        for (Record record : records) {
            this.save(record);
        }


    }
}
